﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Csla.Data;

namespace Accounting.Library.GhiSo
{
    public class GhiSoBSP_BanDichVu : GhiSoService
    {
        public GhiSoBSP_BanDichVu(object ChungTu) : base(ChungTu) { }

        protected override void LoadThongTinChung()
        {
            base.LoadThongTinChung();

            BSP_BanDichVu parent = (BSP_BanDichVu)ChungTu;

            LoaiChungTuId = BusinessConfiguration.Configuration.LoaiChungTuId_BSP_BanDichVu;
            dongTien = DongTien.Vao;
            PhuongThucThanhToanId = parent.PhuongThucThanhToanId;
            ChungTuGocId = parent.Khoa;
            NgayChungTu = parent.NgayChungTu;
            NgayHachToan = parent.NgayHachToan;
            NhaCungCapId = parent.NhaCungCapId;
            SoHD = parent.Ma;
            double tyGia = parent.TyGia == null ? 1 : (double)parent.TyGia;
            TongTienThanhToanQĐ = parent.TongTienThanhToanQĐ == null ? 0 : (double)parent.TongTienThanhToanQĐ;
            TongTienThanhToan = parent.TongTienThanhToan == null ? 0 : (double)parent.TongTienThanhToan;
        }

        public override void GhiSoChiTiet(ContextManager<Accounting.DataAccess.AccountingDataContext> ctx)
        {
            BSP_BanDichVu parent = (BSP_BanDichVu)ChungTu;

            // Neu la ngoai te thi cap nhat tai khoan ngoai bang
            if (!LoaiTienTeService.LaLoaiTienTeChinh(ctx, (int)parent.LoaiTienId))
            {
                GhiSoTaiKhoanNgoaiBang(ctx, parent.LoaiTienId);
            }

            #region BSP_BanDichVu_ChiTiet
            foreach (BSP_BanDichVu_ChiTietChild child in parent.BSP_BanDichVu_ChiTiets)
            {
                if (!child.ThanhTienQĐ.Equals(0))
                {
                    //Su dung store procedure nay để add dữ liệu vào trong so chi tiet
                    double soTien = (double)child.ThanhTienQĐ - (double)child.TienCKQĐ;
                    ctx.DataContext.addTH_SoChiTiet(ref Khoa, ChungTuGocId, child.Khoa, LoaiChungTuId,
                        NgayChungTu, NgayHachToan, SoHD, child.TKNoId, child.TKCoId, child.DienGiai, soTien, KyKetChuyenId, BusinessConfiguration.Configuration.CHITIET, null, null);
                }
            }
            #endregion

            #region BSP_BanDichVu_Thue
            //Neu la hoa don gtgt thi moi ghi so tien thue
            if (LoaiHoaDonService.LaHoaDonGiaTriGiaTang(ctx, (int)parent.LoaiHDId))
            {
                foreach (BSP_BanDichVu_ThueChild child in parent.BSP_BanDichVu_Thues)
                {
                    if (!child.TienThueGTGTQĐ.Equals(0))
                    {
                        //Su dung store procedure nay để add dữ liệu vào trong so chi tiet
                        ctx.DataContext.addTH_SoChiTiet(ref Khoa, ChungTuGocId, child.Khoa, LoaiChungTuId,
                            NgayChungTu, NgayHachToan, SoHD, child.TKNoId, child.TKCoId, child.DienGiai, child.TienThueGTGTQĐ, KyKetChuyenId,
                            BusinessConfiguration.Configuration.THUE_GTGT, null, null);
                    }
                }
            }
            #endregion
        }
    }
}
